package com.zonda.sampleretrofit2.view;


import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.zonda.sampleretrofit2.R;

public final class NewsListItem2Layout extends NewsListItemBaseLayout {

  private TextView mTitleTv;
  private final ImageView[] mImageViews = new ImageView[3];
  private NewsListTagView mTagView;

  private int imageSpacing;

  private int mImageWidth = -1;
  private int mImageHeight = -1;

  private String[] mImageUrls;


  public NewsListItem2Layout(Context context, AttributeSet attrs) {
    super(context, attrs);
    imageSpacing = getResources().getDimensionPixelSize(R.dimen.image_padding);
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    //控件总宽度
    int widthHint = View.MeasureSpec.getSize(widthMeasureSpec);
    int availableWidth = widthHint - getPaddingLeft() - getPaddingRight();//此处并不需要减去 Margin
    //图片指定宽度
    final int imageWidth = NewsListItemHelper.calculateSmallImageWidth(availableWidth, getResources());
    final int imageHeight = (int) (imageWidth / NewsListItemHelper.SMALL_IMAGE_RATIO);

    mImageWidth = imageWidth;
    mImageHeight = imageHeight;

    int heightHint = 0;
    measureChildWithMargins(mTitleTv, widthMeasureSpec, 0, heightMeasureSpec, 0);
    final int titleHeight = mTitleTv.getMeasuredHeight();
    heightHint += titleHeight;
    for (int i = 0; i < mImageViews.length; i++) {
      final ImageView childImageView = mImageViews[i];
      final LayoutParams childImageParams = (LayoutParams) childImageView.getLayoutParams();
      childImageParams.width = imageWidth;
      childImageParams.height = imageHeight;
      childImageParams.leftMargin = i * (imageWidth + imageSpacing);
      childImageParams.topMargin = titleHeight;
      measureChildWithMargins(childImageView, widthMeasureSpec, 0, heightMeasureSpec, 0);
      heightHint += imageHeight;
    }
    LayoutParams tagViewParams = (LayoutParams) mTagView.getLayoutParams();
    tagViewParams.topMargin = titleHeight + imageHeight;
    measureChildWithMargins(mTagView, widthMeasureSpec, 0, heightMeasureSpec, 0);
    final int tagViewHeight = mTagView.getMeasuredHeight();
    heightHint += tagViewHeight + getPaddingTop() + getPaddingBottom();

    setMeasuredDimension(resolveSize(widthHint, widthMeasureSpec),
        resolveSize(heightHint, heightMeasureSpec));
  }

  @Override
  void onInitChildView() {
    if (getChildCount() != 5) {
      return;
    }
    mTitleTv = (TextView) getChildAt(0);
    mImageViews[0] = (ImageView) getChildAt(1);
    mImageViews[1] = (ImageView) getChildAt(2);
    mImageViews[2] = (ImageView) getChildAt(3);
    mTagView = (NewsListTagView) getChildAt(4);
  }

  public void setImageUrls(String[] imageUrls) {

    for (int i = 0; i < mImageViews.length; i++) {

      Glide.with(getContext().getApplicationContext())
          .load(imageUrls[i])
          .listener(new RequestListener<String, GlideDrawable>() {
            @Override
            public boolean onException(Exception e, String model,
                                       Target<GlideDrawable> target, boolean isFirstResource) {
              return false;
            }

            @Override
            public boolean onResourceReady(GlideDrawable resource, String model,
                                           Target<GlideDrawable> target,
                                           boolean isFromMemoryCache,
                                           boolean isFirstResource) {
              return false;
            }
          }).into(mImageViews[i]);
    }
    mImageUrls = imageUrls;
  }

  public void setTitleText(String titleText) {
    mTitleTv.setText(titleText);
  }

}










